package com.steven.ourcontest.contest.view

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.steven.ourcontest.R
import com.steven.ourcontest.app.AppRouter
import com.steven.ourcontest.base.BaseFragment
import com.steven.ourcontest.contest.adapter.ContestGuessAdapter
import com.steven.ourcontest.contest.adapter.ContestHistoryAdapter
import com.steven.ourcontest.contest.bean.MatchInfoBanner
import com.steven.ourcontest.contest.viewmodel.ContestViewModel
import com.steven.ourcontest.utils.DateUtil
import kotlinx.android.synthetic.main.fragment_contest.*

class ContestFragment : BaseFragment() {

    private val contestViewModel: ContestViewModel by viewModels()
    private val guessAdapter by lazy {
        context?.let { ContestGuessAdapter(it) }
    }
    private val historyAdapter by lazy {
        context?.let {
            ContestHistoryAdapter(it)
        }
    }

    override fun getViewId(): Int? {
        return R.layout.fragment_contest
    }

    override fun initSmartRefresh(): SmartRefreshLayout? {
        return smart_refresh
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
    }

    private fun initView() {
        rv_guess?.adapter = guessAdapter
        rv_recent?.adapter = historyAdapter
        rv_recent?.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        contestViewModel.matchHistoryRecommendRes.observe(viewLifecycleOwner, Observer {
            if (smart_refresh?.isRefreshing == true) {
                smart_refresh?.finishRefresh()
            }
            if (it != null) {
                tv_recent?.isVisible = true
                rv_recent?.isVisible = true
                historyAdapter?.listData?.clear()
                historyAdapter?.listData?.addAll(it.data)
                historyAdapter?.notifyDataSetChanged()
            } else {
                tv_recent?.isVisible = false
                rv_recent?.isVisible = false
            }
        })
        banner_hot?.loadImage { banner, model, view, position ->
            if (model is MatchInfoBanner) {
                val tv_match_name: TextView? = view.findViewById(R.id.tv_match_name)
                val tv_belong: TextView? = view.findViewById(R.id.tv_belong)
                val tv_signup_time: TextView? = view.findViewById(R.id.tv_signup_time)
                val tv_match_time: TextView? = view.findViewById(R.id.tv_match_time)
                val tv_readNum: TextView? = view.findViewById(R.id.tv_readNum)
                tv_match_name?.text = model.team.name
                model.team.belong?.let {
                    tv_belong?.text = "主办方：${it}"
                }
                tv_signup_time?.text = "报名时间：${if (model.team.signUpStartTime != 0L && model.team.signUpEndTime != 0L) {
                    DateUtil.timeStamp2Date(
                        model.team.signUpStartTime?.times(1000),
                        "yyyy.MM.dd"
                    ) + "~" + DateUtil.timeStamp2Date(model.team.signUpEndTime?.times(1000), "yyyy.MM.dd")
                } else {
                    "暂无"
                }}"
                tv_match_time?.text = "比赛时间：${if (model.team.matchStartTime != 0L && model.team.matchEndTime != 0L) {
                    DateUtil.timeStamp2Date(
                        model.team.matchStartTime?.times(1000),
                        "yyyy.MM.dd"
                    ) + "~" + DateUtil.timeStamp2Date(model.team.matchEndTime?.times(1000), "yyyy.MM.dd")
                } else {
                    "暂无"
                }}"
                tv_readNum?.text = model.team.sum.toString()
                view.setOnClickListener {
                    context?.let { it1 -> AppRouter.openMatchDetailActivity(it1, model.team.id) }
                }
            }
        }
        contestViewModel.matchHotRecommendRes.observe(viewLifecycleOwner, Observer {
            if (smart_refresh?.isRefreshing == true) {
                smart_refresh?.finishRefresh()
            }
            if (it != null) {
                val matchList = ArrayList<MatchInfoBanner>()
                it.data.forEach {
                    matchList.add(MatchInfoBanner(it))
                }
                banner_hot?.setBannerData(R.layout.item_match_list, matchList)
            }
        })
        contestViewModel.matchRecommendRes.observe(viewLifecycleOwner, Observer {
            if (smart_refresh?.isRefreshing == true) {
                smart_refresh?.finishRefresh()
            }
            if (it != null) {
                tv_guess?.isVisible = true
                rv_guess?.isVisible = true
                guessAdapter?.listData?.clear()
                guessAdapter?.listData?.addAll(it.data)
                guessAdapter?.notifyDataSetChanged()
            } else {
                tv_guess?.isVisible = false
                rv_guess?.isVisible = false
            }
        })
        smart_refresh?.setOnRefreshListener {
            contestViewModel.searchHistory()
            contestViewModel.searchTop10MatchRecent()
            contestViewModel.queryMatch()
        }
        contestViewModel.searchHistory()
        contestViewModel.searchTop10MatchRecent()
        contestViewModel.queryMatch()
    }

    override fun login() {
        contestViewModel.searchHistory()
        contestViewModel.searchTop10MatchRecent()
        contestViewModel.queryMatch()
    }
}